<template>
  <div :class="styles.page">
    <div :class="styles['login-area']">
      <el-form :model="formData" status-icon ref="loginForm">

        <el-form-item>
          正在跳转中。。。。。
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
    import styles from './login.scss'
    import {get, findKey} from 'lodash'
    import {loginFromMainSys} from '@/api/module/login.js'
    import {COOKIE_LIST, UNIT_TYPE} from '@/util/constant'
    import {setCookie} from '@/util/help'
    import {Decrypt,Encrypt} from '@/util/AESUtil.js'

    export default {
        data() {
            return {
                styles,
                formData: {
                    username: '',
                    password: ''
                },
                token: '',
                showLoginLoading: false,
                Token01: ''
            }
        },
        mounted () {
               // alert('logim')
                console.log(this.formData.username)
              this.getToken()
              this.submitLogin('loginForm')
        },
        methods: {
            getToken() {
                var url = window.location.href
                var splitUrl=url.split('?')[1].toString()
                var username=splitUrl.split('_')[0].toString()
                var psw=splitUrl.split("_")[1].toString()
                //将从后端取得的字符串转码
                var loginname=decodeURIComponent(username)
                var loginpsw=decodeURIComponent(psw)
                //将后端传来的用户名解码（AES）并赋值到表单
                let uname=Decrypt(loginname)
                let upsw=Decrypt(loginpsw)
                console.log(uname)
                console.log(upsw)
                this.formData.username= uname
                this.formData.password=upsw
            },
            submitLogin(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.showLoginLoading = true
                        loginFromMainSys(this.formData).then((res) => {
                            let token = get(res, 'data.token')
                            //alert(token)
                            if (token) {
                                setCookie(COOKIE_LIST.AUTHORIZATION, token)
                                setCookie(COOKIE_LIST.MENU, get(res, 'data.menu', []))
                                this.setUnitTypeCookie(get(res, 'data.data', []))
                                this.$router.push('/data_classification')
                            } else {
                                this.showLoginLoading = false
                                this.formData.password = ''
                                this.$message.error(get(res, 'data.msg', ''))
                            }
                        })
                    }
                })
            },
            setUnitTypeCookie(unitType) {
                let result = {}
                unitType.forEach((type) => {
                    let metaUnitCategory = findKey(UNIT_TYPE, function (o) {
                        return o === type['metaUnitCategory']
                    })
                    result[metaUnitCategory] = type['metaUnitCategoryValue']
                })
                setCookie(COOKIE_LIST.UNIT, result)
            }
        }
    }
</script>
